package com.icehand.payment.api.v1.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.icehand.common.constants.CommonConstants;
import com.icehand.common.enums.CommonStatusEnum;
import com.icehand.common.enums.PayChannelEnum;
import com.icehand.common.result.BizResult;
import com.icehand.mybatis.payment.domain.AppChannel;
import com.icehand.mybatis.payment.mapper.AppChannelMapper;
import com.icehand.mybatis.payment.mapper.AppMapper;
import com.icehand.payment.api.v1.service.PayChannelServiceV1;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.CacheNamespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author icehand
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "pay-channels")
public class PayChannelServiceImplV1 implements PayChannelServiceV1 {
    private final AppMapper appMapper;
    private final AppChannelMapper appChannelMapper;

    @Autowired
    public PayChannelServiceImplV1(AppChannelMapper appChannelMapper, AppMapper appMapper) {
        this.appChannelMapper = appChannelMapper;
        this.appMapper = appMapper;
    }

    /**
     * 返回的结构:[{"name":"支付宝","channels":[{"channelId":1,"tag":"alipay_wap","desc":"支付宝WAP支付"}]}]
     */
    @Override
    @Cacheable(key = "'appId:'+ #appId")
    public BizResult payChannels(Integer appId) {
        List<AppChannel> list = appChannelMapper.selectList(new EntityWrapper<AppChannel>()
                .eq("app_id",appId)
                .eq("status", CommonStatusEnum.NORMAL.getStatus()));
        Map<String,List<PayChannelEnum>> map = Maps.newHashMap();
        for(AppChannel appChannel : list){
            PayChannelEnum payChannelEnum = PayChannelEnum.choiceByChannelId(appChannel.getChannelId());
            if(map.containsKey(payChannelEnum.getName())){
                map.get(payChannelEnum.getName()).add(payChannelEnum);
            }else{
                List<PayChannelEnum> payChannelEnums = new ArrayList<>();
                payChannelEnums.add(payChannelEnum);
                map.put(payChannelEnum.getName(),payChannelEnums);
            }
        }

        List<Map<String,Object>> result = new ArrayList<>();
        map.forEach((k,v)->{
            Map<String,Object> m = Maps.newHashMap();
            m.put("name", k);

            List<Map<String,Object>> channels = Lists.newArrayList();
            v.forEach(payChannelEnum -> {
                channels.add(PayChannelEnum.map(payChannelEnum));
            });
            m.put("channels",channels);
            result.add(m);
        });

        return new BizResult<>(result);
    }

    /**
     * 根据appId和channelId获取到对应的配置信息
     */
    @Override
    @Cacheable(key = "'config:'+#appId+'-'+#channelId")
    public JSONObject payChannelConfig(Integer appId, Integer channelId) {
        AppChannel appChannel = appChannelMapper.selectOne(new AppChannel().setAppId(appId).setChannelId(channelId));
        return JSONObject.parseObject(appChannel.getConfig());
    }
}
